package api

import (
	"github.com/gin-gonic/gin"
	"monitoring-api/models"
	"monitoring-api/service"
)

// UploadErr 上报异常
func UploadErr(c *gin.Context) {
	var service service.UploadErrService
	if err := c.ShouldBind(&service); err == nil {
		if models.WebIsExist(service.WebID) {
			res := service.Upload()
			c.JSON(200, res)
		} else {
			c.JSON(404, gin.H{
				"code":        404,
				"message":     "该网站不存在",
				"upload_data": service,
			})
		}
	} else {
		c.JSON(404, gin.H{
			"code":    404,
			"message": err.Error(),
		})
	}
}

// UploadPer 上报性能信息
func UploadPer(c *gin.Context) {
	var service service.UploadPerService
	if err := c.ShouldBind(&service); err == nil {
		if models.WebIsExist(service.WebID) {
			res := service.Upload()
			c.JSON(200, res)
		} else {
			c.JSON(404, gin.H{
				"code":        404,
				"message":     "该网站不存在",
				"upload_data": service,
			})
		}
	} else {
		c.JSON(404, gin.H{
			"code":    404,
			"message": err.Error(),
		})
	}
}

// UploadAction 上报用户行为信息
func UploadAction(c *gin.Context) {
	var service service.UploadActionService
	if err := c.ShouldBind(&service); err == nil {
		if models.WebIsExist(service.WebID) {
			res := service.Upload()
			c.JSON(200, res)
		} else {
			c.JSON(404, gin.H{
				"code":        404,
				"message":     "该网站不存在",
				"upload_data": service,
			})
		}
	} else {
		c.JSON(404, gin.H{
			"code":    404,
			"message": err.Error(),
		})
	}
}

// UploadReq 上报请求信息
func UploadReq(c *gin.Context) {
	var service service.UploadReqService
	if err := c.ShouldBind(&service); err == nil {
		if models.WebIsExist(service.WebID) {
			res := service.Upload()
			c.JSON(200, res)
		} else {
			c.JSON(404, gin.H{
				"code":        404,
				"message":     "该网站不存在",
				"upload_data": service,
			})
		}
	} else {
		c.JSON(404, gin.H{
			"code":    404,
			"message": err.Error(),
		})
	}
}
